var state = "idle";
var result;

function getUsername(){
	return localStorage['username'];
}

function getPassword(){
	return localStorage['password'];
}

function setUsername(username){
	localStorage['username'] = username;
}

function setPassword(password){
	localStorage['password'] = password;
}

function getResultString(){
	return result;
}

function setState(s){
	state = s;
}

function IPGW(username, password, operation){
	this.username = username;
	this.password = password;
	this.operation = operation;
}

IPGW.prototype = {
    "magicString" : "|;kiDrqvfi7d$v0p5Fg72Vwbv2;|",
	"exec" : function(){
		state = "working";
		var ntf_inprogress = showMsg(chrome.i18n.getMessage("info_inprogress"), "", -1);
		var xml = null;
		var noerr = true;
		var needLogin = true;
		var errtitle = chrome.i18n.getMessage("err_inprogress");
		var errmsg = null;
		// check the current status of netportal
		$.ajax("https://its.pku.edu.cn/netportal/", {
			"async" : false,
			"cache" : false,
			"error" : function(xhr, status, err){
				noerr = false;
				errtitle = status;
				errmsg = err;
			},
			"success" : function(data){
				xml = data;
				if(xml.indexOf('您来自校外') > 0){
					console.debug("not in school");
					errmsg = chrome.i18n.getMessage("err_notinpku");
					noerr = false;
				} else if(xml.indexOf("来自校内") > 0){
					// the user may has been logined
					console.debug("logined, in school");
					if(xml.indexOf(this.username) > 0){
						console.debug("username matches");
						needLogin = false;
					} else {
						// this is a bug need to be fixed, as the app should logout then login in this situation
						console.debug("username doesn't match");
						needLogin = false;
					}
				}
			},
		});
		if(noerr){
			if(needLogin){
				console.debug("try login as " + this.username);
				$.ajax("https://its.pku.edu.cn/cas/login", {
					"type" : "POST",
					"async" : false,
					"cache" : false,
					"data" : {
						'username1': this.username,
						'password': this.password,
						'fwrd':'free',
						'pwd_t':'密码',
						'username':this.username + this.magicString + this.password + this.magicString + '12'
					},
					"error" : function(xhr, status, err){
						noerr = false;
						errtitle = status;
						errmsg = err;
					},
					"success" : function(data){
						xml = data;
						if(xml.indexOf("javascript:doaction('ipgwopen',true)") < 0){
							console.debug("login failed");
							errmsg = chrome.i18n.getMessage("err_login");
							noerr = false;
						} else {
							console.debug("login succeeded");
						}
					}
				});
			}

			if(noerr){
				var url = "https://its.pku.edu.cn/netportal/" + this.operation;
				console.debug(url);
				$.ajax(url, {
					"async" : false,
					"cache" : false,
					"error" : function(xhr, status, err){
						noerr = false;
						errtitle = status;
						errmsg = err;
					},
					"success" : function(data){
						xml = data;
						var pat = /<!--IPGWCLIENT_START (.*) IPGWCLIENT_END-->/;
						var r = pat.exec(xml);
						if(r){
							result = r[1].replace(/\s+/g, '\n').replace(/\n/g, '<br/>');
							console.debug(result);
						} else {
							setLastError("cannot do the operation", xml);
							console.warn(xml);
							noerr = false;
							errmsg = "could not find information field";
						}
					}
				});
			}
		}

		state = "idle";
		ntf_inprogress.cancel();
		if(!noerr){
			showMsg(errtitle, errmsg);
		} else {
			var notification = webkitNotifications.createHTMLNotification("result.html");
			notification.show();
			setTimeout(function (){
				notification.cancel();
			}, msgTimeout);
		}
	}
}